// --------------------------------------------------------------------------------------------------------------------
// <copyright file="Match.cs" company="Roman '4ux-nbIx' Novitsky">
//   Copyright (c) Roman '4ux-nbIx' Novitsky 2010
// </copyright>
// <summary>
//   Defines the Match type.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace MtgMC.DataModel
{
    #region Namespace Imports

    using System;
    using System.Linq;

    using Json;

    #endregion


    /// <summary>
    /// Describes and manipulates the match.
    /// </summary>
    public class Match
    {
        #region Constants and Fields

        private readonly MatchRoundCollection _rounds = new MatchRoundCollection();

        #endregion


        #region Properties

        /// <summary>
        /// Gets or sets the event.
        /// </summary>
        /// <value>
        /// The event.
        /// </value>
        [JsonType(typeof(CustomGame))]
        [JsonType(typeof(Tournament))]
        public GameEvent Event
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the opponent.
        /// </summary>
        /// <value>
        /// The opponent.
        /// </value>
        public Player Opponent
        {
            get;
            set;
        }

        /// <summary>
        /// Gets or sets the opponent deck.
        /// </summary>
        /// <value>
        /// The opponent deck.
        /// </value>
        public Deck OpponentDeck
        {
            get;
            set;
        }

        /// <summary>
        /// Gets the rounds.
        /// </summary>
        /// <value>
        /// The rounds.
        /// </value>
        public MatchRoundCollection Rounds
        {
            get
            {
                return _rounds;
            }
        }

        /// <summary>
        /// Gets or sets the start time.
        /// </summary>
        /// <value>
        /// The start time.
        /// </value>
        public DateTime StartTime
        {
            get;
            set;
        }

        /// <summary>
        /// Gets a value indicating whether this <see cref="Match"/> was won.
        /// </summary>
        /// <value>
        /// <c>True</c> if this <see cref="Match"/> was won; otherwise, <c>false</c>.
        /// </value>
        public bool WasWon
        {
            get
            {
                int roundsLost = Rounds.Where(r => r.Winner == Opponent).Count();
                int roundsWon = Rounds.Count - roundsLost;

                return roundsWon > roundsLost;
            }
        }

        #endregion
    }
}